{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "7600c4b5-89d4-48ef-83e6-3d50beda64bd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "测试集数据的预测标签为 [0 1 1 0 2 2 2 0 0 2 1 0 2 1 1 0 1 1 0 0 1 1 2 0 2 1 0 0 1 2 1 2 1 2 2 0 1\n",
      " 0 1 2 2 0 1 2 1 2 0 0 0 1]\n",
      "测试集数据的真实标签为 [0 1 1 0 2 1 2 0 0 2 1 0 2 1 1 0 1 1 0 0 1 1 1 0 2 1 0 0 1 2 1 2 1 2 2 0 1\n",
      " 0 1 2 2 0 2 2 1 2 0 0 0 1]\n",
      "测试集共有50条数据，其中预测错误的数据有3条，预测准确率为0.94\n"
     ]
    }
   ],
   "source": [
    "from sklearn.datasets import load_iris \n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.naive_bayes import GaussianNB\n",
    "from sklearn.metrics import accuracy_score\n",
    "x,y=load_iris().data,load_iris().target\n",
    "x_train,x_test,y_train,y_test=train_test_split(x,y,random_state=1,test_size=50)\n",
    "model=GaussianNB()\n",
    "model.fit(x_train,y_train)\n",
    "pred=model.predict(x_test)\n",
    "print(\"测试集数据的预测标签为\",pred)\n",
    "print(\"测试集数据的真实标签为\",y_test)\n",
    "print(\"测试集共有%d条数据，其中预测错误的数据有%d条，预测准确率为%.2f\"%(x_test.shape[0],(pred!=y_test).sum(),accuracy_score(y_test,pred)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8353ffc7-590f-4d65-ab80-32249f622ca0",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklean.naive.naivve_bayes import GaussianNB\n",
    "from sklearn.naive_bayes import MultinomialNB\n",
    "from sklearn.naive_bayes import BernoulliNB"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "fca714b0-7dc0-4063-adc0-af1124150400",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "正常邮件的文件列表 ['normal-mail1.txt', 'normal-mail2.txt', 'normal-mail3.txt', 'normal-mail4.txt', 'normal-mail5.txt', 'normal-mail6.txt', 'normal-mail7.txt', 'normal-mail8.txt', 'normal-mail9.txt']\n",
      "垃圾邮件的文件列表 ['spam-mail1.txt', 'spam-mail2.txt', 'spam-mail3.txt', 'spam-mail4.txt', 'spam-mail5.txt', 'spam-mail6.txt', 'spam-mail7.txt', 'spam-mail8.txt', 'spam-mail9.txt']\n",
      "停用词文件内容: ['啊', '阿', '哎', '哎呀', '唉', '于是', '还']\n"
     ]
    }
   ],
   "source": [
    "import os \n",
    "normalFileList=os.listdir(\".//item5-ss-data/normal/\")\n",
    "spamFileList=os.listdir(\".//item5-ss-data/spam/\")\n",
    "print(\"正常邮件的文件列表\",normalFileList)\n",
    "print(\"垃圾邮件的文件列表\",spamFileList)\n",
    "stopList=[]\n",
    "for line in open(\".//item5-ss-data/stopwords.txt\",encoding='utf-8'):\n",
    "    stopList.append(line[:len(line)-1])\n",
    "print(\"停用词文件内容:\",stopList)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "60903bcc-baae-4eb5-87e4-1fb625472813",
   "metadata": {},
   "outputs": [],
   "source": [
    "from collections import Counter \n",
    "from itertools import chain \n",
    "allwords=[]\n",
    "for spamfile in spamFileList:\n",
    "    word=getWords(\".//item5/item5-ss-data/spam/\"+spamfile,stopList)\n",
    "    allwords.append(words)\n",
    "for normalfile in spamFileList:\n",
    "    words=getWords(\".//item5/item5-ss-data/spam/\"+spamfile,stopList)\n",
    "    allwords.append(words)\n",
    "print(\"训练集中所出现的有效词语列表:\")\n",
    "print(allwords)\n",
    "frep=Countre(chain(*allwords))\n",
    "topTen=frep.most_common(10)\n",
    "topwords=[w[0] for w in topTen]\n",
    "print(\"训练集中出现的频次最高的qia\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c3599988-0436-455b-8190-40d82be38ca9",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
